/*
 *  This file is part of Cube.
 *
 *  Cube is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Cube is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Cube.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package cube.charts;


import cube.charts.cubedesign.Grid;
import cube.charts.cubedesign.Colour;
import cube.charts.cubedesign.BW;
import cube.charts.cubedesign.Designable;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;

/**
 *
 * @author akermanl
 */

public class Cube extends JPanel {     
    
  private double p[][] = new double[9][4];
  private int x=1;
  private int y=2;
  private int z=3;
  private ChartManager chartManager;
  
  public Cube(ChartManager chartManager){
      super();
      this.chartManager=chartManager;
      
      initPoints();
      
  }              
    
  public void initPoints(){
      // 8 Eckpunkte im lokalen Würfel-Koordinatensystem
        // Nullpunkt = Mittelpunkt x=1, y=2, z=3
        p[1][x] = -100; p[1][y] = -100; p[1][z] = -100;
        p[2][x] = +100; p[2][y] = -100; p[2][z] = -100;
        p[3][x] = +100; p[3][y] = -100; p[3][z] = +100;
        p[4][x] = -100; p[4][y] = -100; p[4][z] = +100;
        p[5][x] = -100; p[5][y] = +100; p[5][z] = -100;
        p[6][x] = +100; p[6][y] = +100; p[6][z] = -100;
        p[7][x] = +100; p[7][y] = +100; p[7][z] = +100;
        p[8][x] = -100; p[8][y] = +100; p[8][z] = +100;
        
        // y-Werte spiegeln
            for (int j=1;j<9;j++) {
                p[j][y] = -p[j][y];
            }

        //       8 - - - - - 7
        //     / |         / |
        //    5 - - - - - 6  |
        //    |  |        |  |
        //    |  4 - - - -|- 3
        //    | /         | /
        //    1 - - - - - 2
  }
  
  @Override 
  protected void paintComponent( Graphics g ) 
  { 
      
      //super.paintComponent( g );
        Graphics2D g2 = (Graphics2D) g; 
        g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, 
                         RenderingHints.VALUE_ANTIALIAS_ON); 

    
        g.clearRect(0,0, 500, 500);
       
        Designable design = null;
        
        if (((CubeChart) chartManager.getHashtableOfCharts().get("ANC")).getDesign().equals("grid")){
            design = new Grid();
        } 
        else if (((CubeChart) chartManager.getHashtableOfCharts().get("ANC")).getDesign().equals("colour")){
            design = new Colour();
        } 
        else if (((CubeChart) chartManager.getHashtableOfCharts().get("ANC")).getDesign().equals("bw")){
            design = new BW();
         }               
        design.setG(g);
        design.setP(p);
        design.paint();
        
  } 
  

  public void setAngles(double angle_x,double angle_y,double angle_z){
        initPoints();
        rotate(angle_y,angle_z,angle_x);
      
      
  }
 
  public void rotate(double deltaAngle_x,double deltaAngle_y,double deltaAngle_z){
       
      
        double px, py, pz;

        for (int i=1;i<9;i++) {

            px = p[i][x];
            py = p[i][y];
            pz = p[i][z];

            // Rotation um x-Achse
            p[i][y] = py*Math.cos(deltaAngle_x)-pz*Math.sin(deltaAngle_x);
            p[i][z] = py*Math.sin(deltaAngle_x)+pz*Math.cos(deltaAngle_x);

            py = p[i][y];
            pz = p[i][z];

            // Rotation um y-Achse
            p[i][x] = px*Math.cos(deltaAngle_y)+pz*Math.sin(deltaAngle_y);
            p[i][z] =-px*Math.sin(deltaAngle_y)+pz*Math.cos(deltaAngle_y);

            px = p[i][x];

            // Rotation um z-Achse
            p[i][x] = px*Math.cos(deltaAngle_z)-py*Math.sin(deltaAngle_z);
            p[i][y] = py*Math.cos(deltaAngle_z)+px*Math.sin(deltaAngle_z);
            
            this.repaint();
            
            
        }
    }
}


